package calao.client;

import java.util.Iterator;
import java.util.Vector;

import calao.client.ui.Form;

import com.google.gwt.user.client.ui.CheckBox;

class ApplicationsForm
extends Form
{
	
	static class Box extends CheckBox {
		DesktopFeature feature;
	}
	
	final Desktop desktop = Desktop.getDesktop();
	final Box[] boxes;
	
	ApplicationsForm()
	{
		boxes = new Box[DesktopFeature.count()]; 
		Iterator iter = DesktopFeature.MAP.values().iterator();
		int count = 0;
		while (iter.hasNext()) {
			DesktopFeature feature = (DesktopFeature)iter.next();
			Box box = new Box();
			boxes[count++] = box;
			box.feature = feature;
			box.setChecked(feature.active);
			box.setText(feature.getTitle());
			add(null, box);
		}
	}

	public void saveChanges()
	{
		Vector featureList = new Vector();
		Vector appList = new Vector();
		for (int i=0; i<boxes.length; i++) {
			if (boxes[i].isChecked()) {
				DesktopFeature feature = boxes[i].feature;
				String appName = feature.getApplicationName(); 
				featureList.add(feature.getName());
				if (!appList.contains(appName)) appList.add(appName);
			}
		}		
		Preferences prefs = new Preferences();
		prefs.features = (String[])featureList.toArray(new String[0]);
		prefs.applications = (String[])appList.toArray(new String[0]);
		desktop.sessionService.setWorkspacePreferences(prefs, new ServiceCallback() {
			public void onReturn(Object result) {
				setChangesSaved();
				desktop.refresh();
			}
		});
	}

}
